<?php
class EnticeWS
{
	public static $aniSubscriber = null;
	public static $urlBilling3   = 'http://208.74.72.210:8080/EnticeWS/services/billing3?wsdl';
	
	/**
	 * get ani subscriber from webservices
	 */
	public static function getANISubscriber($subscriber_id)
	{
		if(self::$aniSubscriber == null)
		{
			$service             = new SoapClient(self::$urlBilling3);
			$response            = $service->getANISubscriber(CGlobal::$wsResellerId,CGlobal::getResllerKey(),$subscriber_id);//return string
			$response            = simplexml_load_string($response);
			self::$aniSubscriber = $response;
		}
				
		return self::$aniSubscriber ;
	}
	

	/**
	 * Get list phone from anisubscriber
	 */
	public static function getANIPhones($subscriber_id)
	{
		$listPhone = array();
		$response  = self::getANISubscriber($subscriber_id);

		foreach($response->data->row->endpoint as $e)
			$listPhone[] = (string)$e->VALUE;
		
		return $listPhone;
	}
	
	public static function updateANISubscriber($subscriber_id, $listInfo = array())
	{
		$service  = new SoapClient(self::$urlBilling3);

		$xml      = new SimpleXMLElement('<EnticeRequest></EnticeRequest>');		
		$xmldata  = $xml->addChild('data');
			$row  = $xmldata->addChild('row');
	            $row->addChild('SUBSCRIBER_ID', $subscriber_id);
	            $row->addChild('FIRST_NAME',$listInfo['FIRST_NAME'] );
				$row->addChild('LAST_NAME',$listInfo['LAST_NAME'] );
				$row->addChild('ACCT_ID',$listInfo['ACCT_ID']);
				$row->addChild('CUST_ID',$listInfo['CUST_ID']);
				$row->addChild('STATUS',$listInfo['STATUS']);
					$contact = $row->addChild('ANILIST');
						foreach($listInfo['PHONE'] as $index => $phone)
						{
							$ani = $contact->addChild('ANI');
	                        $ani->addChild('NUMBER', $phone->VALUE);
	                        $ani->addChild('PIN', $phone->CARD_PIN);
						}
	     
		$response = $service->updateANISubscriber(CGlobal::$wsResellerId,CGlobal::getResllerKey(),$xmldata->asXML(), $subscriber_id);
		
		return $response;
	}
	
	/**
	 * 
	 * Delete ani phone from subscriber id
	 * @param $subscriber_id
	 * @param $phone
	 * @return SoapReturn
	 */
	public static function deleteANIfromSubscriber($subscriber_id, $phone){
		$service  = new SoapClient(self::$urlBilling3);
		
		$xml      = new SimpleXMLElement('<EnticeRequest></EnticeRequest>');
		$xmldata  = $xml->addChild('data');
			$row  = $xmldata->addChild('row');
				$row->addChild('SUBSCRIBER_ID',$subscriber_id);
	            $ani = $row->addChild('ANI');
	                $ani->addChild('NUMBER',$phone);
	                
	   $response = $service->deleteANIfromSubscriber(CGlobal::$wsResellerId,CGlobal::getResllerKey(),$xmldata->asXML(), CGlobal::$wsResellerName);
	   
	   return $response;
	}

	public static function deleteANISubscriber($subscriber_id)
	{
		$service  = new SoapClient(self::$urlBilling3);

		$response = $service->deleteANISubscriber(CGlobal::$wsResellerId,CGlobal::getResllerKey(),$subscriber_id, CGlobal::$wsResellerName);//return SoapReturn object

		return $response;
	}
}